/*********** NOTES HEADER *********** 

DESCRIPTION: This program isolates the diabetes outcomes. 
	It is added to the analysis dataset in program "17.CorrectDiabetes.do"

datasets used:
	(1) /bene_icd_all_`yr'

datasets created:
	(1) tempdiab`yr'
	(2) tempdiabcompl2005_2012
		
************************************/ 



set more off
capture log close
clear all

global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global cpath "N:\MedicareClaims-P045601-BE\Work"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2
adopath +  N:\SIL-Common\reghdfe-master\package


	foreach yr of numlist 2005(1)2012 {
		use BENE_ID date diab* using "$dataOut/bene_icd_all_`yr'.dta", replace
		save "$dataOut/tempdiab`yr'"
		clear
	}
 
	use  "$dataOut/tempdiab2005"
		append using "$dataOut/tempdiab2006"
		append using "$dataOut/tempdiab2007"
		append using "$dataOut/tempdiab2008"
		append using "$dataOut/tempdiab2009"
		append using "$dataOut/tempdiab2010"
		append using "$dataOut/tempdiab2011"
		append using "$dataOut/tempdiab2012"

		gen month=3 if month(date)<4
		 replace month=6 if month(date)>=4 & month(date)<7
		 replace month=9 if month(date)>=7 & month(date)<10
		 replace month=12 if month(date)>9
		gen year=year(date)

		gen quarter=mdy(month,1,year)
		format quarter %td
		drop month year 

		drop diab_compl2
		gen diab_compl2= diab_compl==2

		collapse (max) diab*, by(BENE_ID quarter)
		save  "$dataOut/tempdiabcompl2005_2012", replace
